package com.yeung.actions;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.yeung.actions.ServiceResult.SUCCESS;


/**
 * 案例由idea 编写
 * 请求的测试使用了 RestServices 插件(安装完重启后在view->toolWindows中打开),
 * 请自行安装插件进行测试
 */

@RequestMapping("/parse")
@Controller
public class ParseParams {

    /**
     * rest 风格的url
     * 以crud为例:
     * 新增:/order post
     * 修改:/order/1 put
     * 获取:/order/1 get
     * 删除:/order/1 delete
     * 如何发送put请求和delete请求
     * 1. 需要配置mhiddenHttpethodFilter
     * 2. 需要怕发送post请求
     * 3. 需要在post发送请求时,携带一个name="-method" 的隐藏域,值为DELETE 或者PUT
     */

    /**
     * PathVariable 可以将占位符映射成数据变量
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public String get(@PathVariable("id") Integer id) {
        System.out.println("get test " + id);
        return SUCCESS;
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.PUT)
    public String put(@PathVariable("id") Integer id) {
        System.out.println("put test " + id);
        return SUCCESS;
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String post() {
        System.out.println("post test");
        return SUCCESS;
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id) {
        System.out.println("delete " + id);
        return SUCCESS;
    }

    /**
     * 解析请求头数据中的参数
     *
     * @param un
     * @param psw
     */
    @RequestMapping("/params")
    public void params(@RequestParam("userName") String un,
                       @RequestParam(value = "pass", required = false, defaultValue = "kangkang") String psw) {
        System.out.println("userName= " + un + " pass= " + psw);

    }

    /**
     * @param sessionId
     * @CookieValue 以cookie的id名字取值
     * 用得比较少
     */
    @RequestMapping("cookieVal")
    public void testCookie(@CookieValue("JSESSIONID") String sessionId) {

        System.out.println("sessionId= " + sessionId);
    }

}
